package pers.vinsonws.jianghu.clientwin.service;

import pers.vinsonws.jianghu.clientwin.global.Info;
import pers.vinsonws.jianghu.codeCompont.MsgEveryPro;
import pers.vinsonws.jianghu.tools.MergeMessage;

/**
 * @auther vinsonws
 * @date 2018/11/11 23:22
 */
public class UserService {

    public static boolean register(String userName, String password, String passwordre){
        if (!password.equals(passwordre)) return false;
        Info.uid = "";
        MsgEveryPro.Message.Builder builder = MsgEveryPro.Message.newBuilder();
        builder.setDateLong(System.currentTimeMillis());
        builder.setType(1);
        String[] ss= {userName, password};
        builder.setMessage(MergeMessage.infoToMessage(ss, 1));
        Info.channel.writeAndFlush(builder.build());
        boolean flag = false ;
        //在接收那必须要有个判断让info.uid = uid
        for (int i=0;i<3;i++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (Info.uid.length() > 1){
                flag = true;
                break;
            }
        }
        return flag;
    }

    public static boolean login(String uid, String password){
        Info.status = 0;
        boolean flag = false ;
        MsgEveryPro.Message.Builder builder = MsgEveryPro.Message.newBuilder();
        builder.setType(2);
        builder.setDateLong(System.currentTimeMillis());
        String[] ss = {uid, password};
        builder.setMessage(MergeMessage.infoToMessage(ss, 2));
        Info.channel.writeAndFlush(builder.build());
        //在接收那必须要有个判断让info.status = 1
        for (int i=0;i<3;i++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (Info.status == 1){
                flag = true;
                break;
            }
        }
        return flag;
    }
}
